package com.kws.tool.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.tool.dto.CreateMeasureIndicatorRq;
import com.kws.tool.dto.info.QueryMeasureIndicatorInfo;
import com.kws.tool.entity.TbMeasureIndicator;
import com.kws.tool.mapper.MeasureIndicatorMapper;
import com.kws.tool.service.MeasureIndicatorService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <b>MeasureIndicatorServiceImpl</b>
 *
 * @description: MeasureIndicatorServiceImpl <br>
 * @date: 2024/11/21 13:12 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class MeasureIndicatorServiceImpl extends ServiceImpl<MeasureIndicatorMapper, TbMeasureIndicator> implements MeasureIndicatorService {
    @Override
    public boolean createMeasureIndicator(Long id, List<CreateMeasureIndicatorRq> measureIndicatorList) {
        if (CollectionUtil.isNotEmpty(measureIndicatorList)){
            List<TbMeasureIndicator> indicatorList = new ArrayList<>();
            measureIndicatorList.forEach(item -> {
                TbMeasureIndicator tbMeasureIndicator = new TbMeasureIndicator();
                BeanUtil.copyProperties(item,tbMeasureIndicator);
                tbMeasureIndicator.setFormulaId(id);
                indicatorList.add(tbMeasureIndicator);
            });
            boolean save = this.saveBatch(indicatorList);
            return save;
        }
        return false;
    }

    @Override
    public boolean updateMeasureIndicator(Long id, List<QueryMeasureIndicatorInfo> measureIndicatorList) {
        if (CollectionUtil.isNotEmpty(measureIndicatorList)) {
            List<TbMeasureIndicator> indicatorList = new ArrayList<>();
            measureIndicatorList.forEach(item -> {
                if (item.getId() != null && 0 != item.getId()) {
                    UpdateWrapper updateWrapper = new UpdateWrapper();
                    updateWrapper.eq("id", item.getId());
                    updateWrapper.set("indicator_name", item.getIndicatorName());
                    updateWrapper.set("indicator_type", item.getIndicatorType());
                    updateWrapper.set("indicator_number", item.getIndicatorNumber());
                    int update = baseMapper.update(null, updateWrapper);
                    if (1 != update) {
                        throw new SecurityException("修改测算工具失败！");
                    }
                } else {
                    TbMeasureIndicator tbMeasureIndicator = new TbMeasureIndicator();
                    BeanUtil.copyProperties(item, tbMeasureIndicator);
                    tbMeasureIndicator.setFormulaId(id);
                    indicatorList.add(tbMeasureIndicator);
                }
            });

            if (CollectionUtil.isNotEmpty(indicatorList)) {
                boolean b = this.saveBatch(indicatorList);
                return b;
            }
        }
        return true;
    }
}
